package cn.service.impl;

import cn.entities.MenuInfo;
import cn.entities.RoleInfo;
import cn.entities.RoleMenu;
import cn.mapper.MenuMapper;
import cn.mapper.RoleMapper;
import cn.mapper.RoleMenuMapper;
import cn.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private MenuMapper menuMapper;

    @Override
    public Page<List<RoleInfo>> getRole(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        Page<List<RoleInfo>> page = (Page) roleMapper.selectAll();
        return page;
    }

    @Override
    @Transactional
    public void addRole(RoleInfo roleInfo) {
        roleMapper.insert(roleInfo);
        String auths = roleInfo.getAuths();
        if(auths !=null){
            String[]  arrAuths = auths.split(",");
            Set<String> menuIds = new HashSet<>();
            for (String auth:arrAuths){
                String [] str_auths = auth.split("\\|");
                menuIds.add(str_auths[0]);
                menuIds.add(str_auths[1]);
            }
            for(String mid:menuIds){
                RoleMenu roleMenu = new RoleMenu(null, roleInfo.getId(), Integer.parseInt(mid));
                roleMenuMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public RoleInfo getRoleById(Integer id) {
        RoleInfo roleInfo = roleMapper.selectByPrimaryKey(id);
        List<String > authList = new ArrayList<>();
        List<RoleMenu> rmList = roleMenuMapper.select(new RoleMenu(null, id, null));
        for (RoleMenu rm:rmList){
            MenuInfo menuInfo = menuMapper.selectByPrimaryKey(rm.getMenuId());
            authList.add(rm.getMenuId()+"|"+menuInfo.getPid());
        }
        String auths = StringUtils.collectionToDelimitedString(authList, ",");
        System.out.println("auths: " + auths);
        roleInfo.setAuths(auths);
        return roleInfo;
    }

    @Override
    public void updateRole(RoleInfo roleInfo) {
        roleMapper.updateByPrimaryKey(roleInfo);
        roleMenuMapper.delete(new RoleMenu(null,roleInfo.getId(),null));
        String auths = roleInfo.getAuths();
        if(auths !=null){
            String[]  arrAuths = auths.split(",");
            Set<String> menuIds = new HashSet<>();
            for (String auth:arrAuths){
                String [] str_auths = auth.split("\\|");
                menuIds.add(str_auths[0]);
                menuIds.add(str_auths[1]);
            }
            for(String mid:menuIds){
                RoleMenu roleMenu = new RoleMenu(null, roleInfo.getId(), Integer.parseInt(mid));
                roleMenuMapper.insert(roleMenu);
            }
        }

    }

    @Override
    @Transactional
    public void deletRole(String ids) {
        String [] idsArr = ids.split(",");
        for(String id:idsArr){
            roleMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<RoleInfo> getAllRole() {
        return roleMapper.selectAll();
    }
}
